--
-- @Author:      name
-- @DateTime:    20189-07-30 23:05:48
-- @Description: 所有游戏桌子共用到的消息
local skynet = require("skynet")
local error_code = require("error_code")
local reason = require("reason")
local server_config = dofile(skynet.getenv("server_config"))

local DeskMessage = class("DeskMessage")

---------------------------------------------------------
-- Private
---------------------------------------------------------
local table_insert = table.insert
local table_sort = table.sort
local string_format = string.format

function DeskMessage:ctor(message_dispatch)
	self.message_dispatch = message_dispatch
	self.room_config = nil --房间配置
	self:register()
end

--注册本服务里的消息
function DeskMessage:register()
    --从table_mgr传来的 
	self.message_dispatch:registerSelf('start', handler(self,self.start))
    self.message_dispatch:registerSelf('hotfix', handler(self,self.hotfixFile))	--热更文件
    self.message_dispatch:registerSelf('enter', handler(self,self.enterReq)) 
    self.message_dispatch:registerSelf('disconnect', handler(self,self.disconnect)) --离线
    self.message_dispatch:registerSelf('newmatch_register_all', handler(self,self.registerUser)) --分配后在桌子中创建对应玩家信息  
    self.message_dispatch:registerSelf('exit', handler(self,self.exit)) 
    self.message_dispatch:registerSelf('get_user_playing_info', handler(self,self.getUserPlayingInfo))  
    self.message_dispatch:registerSelf('get_game_type', handler(self,self.getGameType))
    self.message_dispatch:registerSelf('sync_table_stats', handler(self,self.syncTableStats))
    self.message_dispatch:registerSelf('get_player_num', handler(self,self.getPlayerNum))
    self.message_dispatch:registerSelf('stop_server', handler(self,self.closeServer))
    self.message_dispatch:registerSelf('update_coins_on_table', handler(self,self.updateCoins))	
    self.message_dispatch:registerSelf('get_trend_info', handler(self,self.getTrendInfo)) 
    self.message_dispatch:registerSelf('get_summary_online', handler(self,self.getSummaryOnline)) 


    --客户端请求消息
    self.message_dispatch:registerSelf('REQ_CHAT', handler(self,self.chatReq))	
	self.message_dispatch:registerSelf('REQ_USE_MAGIC_PICTURE', handler(self,self.useMagicPictureReq))
	self.message_dispatch:registerSelf('REQ_SITDOWN', handler(self,self.sitDownReq))
	self.message_dispatch:registerSelf('REQ_STANDUP', handler(self,self.standUpReq))
	self.message_dispatch:registerSelf('REQ_LEAVE', handler(self,self.leaveReq))
	self.message_dispatch:registerSelf('REQ_PLAYER_LIST', handler(self,self.playerListReq))
    self.message_dispatch:registerSelf('REQ_GAME_CHANGE', handler(self,self.changeGameReq))
    self.message_dispatch:registerSelf('REQ_COINS_CHANGE', handler(self,self.changeCoinsReq))
    self.message_dispatch:registerSelf('REQ_PERSONAL_INFO', handler(self,self.personalInfoReq))
	self.message_dispatch:registerSelf('REQ_OPEN_CAISHEN', handler(self,self.openCaiShenReq))

    --百人游戏上庄相关
    self.message_dispatch:registerSelf('REQ_BANKER_LIST', handler(self,self.bankerListReq))
    self.message_dispatch:registerSelf('REQ_DO_BANKER', handler(self,self.doBankerReq))
    self.message_dispatch:registerSelf('REQ_UNDO_BANKER', handler(self,self.undoBankerReq))

    --配牌    
    self.message_dispatch:registerSelf('REQ_CONFIG_CARDS', handler(self,self.configCardsReq))
end



---------------------------------------------------------
-- 消息处理
---------------------------------------------------------
--服务初始化
--desk_config:从newmatchsvr传来的配置
function DeskMessage:start(desk_config)
-- {
--         table_id = table_id,
--         table_type = table_type,
--         table_gid = make_table_gid(table_id),
--         payload = payload,
--         game_type = game_type,
--         table_conf = table_conf,
--     })
	dbglog("_____服务初始化________", desk_config)

	self.room_config = global.room_config
	self.user_manager = global.user_manager
	self.desk = global.desk
	self.room_config:init(desk_config) --房间配置

	self.server_id = tonumber(skynet.getenv("server_id"))
 	self.table_id = desk_config.table_id
 	self.table_type = desk_config.table_type
 	self.table_gid = desk_config.table_gid
 	self.game_type = desk_config.game_type
 	self.table_pattern = desk_config.table_pattern
	self.desk_config = desk_config
    self.store_manager = global.store_manager
    self.rule = global.rule
    self.banker_manager = global.banker_manager    
    if self.banker_manager then --上庄管理
        self.banker_manager:start()
    end
    self.bairen_desk = global.bairen_desk
    if self.bairen_desk then 
        self.bairen_desk:start()
    end
    self.user_manager:start()
    self.desk:start()
    global.message_handler:start()
    global.store_manager:init()
    
    self.desk:ntMatchsvrTableStats() --回传状态给newmatchsvr

	return true
end

--热更文件
function DeskMessage:hotfixFile(file_name)
    dbglog("____hotfix_____", file_name)
    return global.hotfix:hotfixFile(file_name)
    
end

--取玩桌子当前玩家个数
function DeskMessage:getPlayerNum()
    return self.user_manager:getUserNum()
end

--取桌子人数状态
function DeskMessage:syncTableStats()
    local table_stats = {}
    table_stats.table_gid = self.table_gid
    table_stats.left_num = self.user_manager:getLeftUserNum()
    table_stats.table_pattern = self.table_pattern
    return self.table_type,table_stats
end

--取游戏类型
function DeskMessage:getGameType()
    errlog("______self.game_type, self.table_type___", self.game_type, self.table_type)
	return self.game_type, self.table_type
end

--取真实玩家个数（不算机器人
function DeskMessage:getUserPlayingInfo()
    local playing_info = {}
    playing_info.game_type = self.game_type
    playing_info.table_type = self.table_type
    playing_info.total = self.user_manager:getRealUserNum()

    return playing_info
end

--断线
function DeskMessage:disconnect(uid, fd)
    dbglog(uid,'disconnect...',fd)
    local user = self.user_manager:getUser(uid)
    if not user or not user:isFD(fd) then --玩家不在这个桌子里
        errlog(uid,'_____disconnect，但玩家不在这个桌子里______invalid status',user:getFD(), fd)
        return false
    end
    user:setDisconnect(true) 
    local ret = self:leaveReq(uid, fd)    
    if self.desk.disconnectCallback then 
        self.desk:disconnectCallback(uid)
    end
    return true
end

--关服
function DeskMessage:closeServer()
    --如果正在玩，下一局不在开始
    --踢出所有玩家
    self.desk:setServiceClose(true)
end

function DeskMessage:touch(key)
end

--退出
function DeskMessage:exit()
    skynet.exit()
end

--更新币值
function DeskMessage:updateCoins(uid, coins, safe_box)
	local user = self.user_manager:getUser(uid)
    user:addCoins(coins)
    user:setSafeBox(safe_box)
    user:reSetInitCoins()
	if user:getSeatId() > 0 then 
		self.desk:sendToNoRobot('table.NTF_MONEY_CHANGED',{chged_list={{uid=uid,coins=user:getCoins()}}})
    else
        self.desk:sendToUser(uid,'table.NTF_MONEY_CHANGED',{chged_list={{uid=uid,coins=user:getCoins()}}})
	end
	return true
end

--大厅取场次信息
function DeskMessage:getTrendInfo(game_type, table_type)
    --珠盘路
    --个人限红
    --桌子状态
    --操作时间
    --场次名字
    local winner_side_list = {}
    local card_type_list = {}
    local win_pool_list = self.desk:getWinPoolList()
    for i=1, #win_pool_list do 
        table_insert(winner_side_list, win_pool_list[i].win_id)
        table_insert(card_type_list, win_pool_list[i].pair_id)
    end
    local room_data = self.room_config:getRoomData()
    local person_limit = room_data.person_limit --个人限红
    local curr_status = self.desk:getHallStateNum()
    local status_time = self.desk:getHallStateTotalTime()    
    local left_time = self.desk:getLeftTime()
    local data = {
        winner_side_list = winner_side_list,
        card_type_list = card_type_list, 
        curr_status = curr_status, --桌子状态
        status_time = status_time,--所有状态初始时间
        left_time = left_time,
        name = room_data.name, --场次名字
        table_type = self.table_type,
        game_type = self.game_type,
        min = person_limit[1], 
        max = person_limit[2], --限红上限
        eject_rate = room_data.eject_rate,
        eject_time = room_data.eject_time,
    }
    -- print("_______data___",data)
    -- dbglog("______1111111____left_tiem___",curr_status, self.desk:getLeftTime())
    return data
end

--player_data_list 接收进桌的玩家信息
--game_type 游戏类型（百人或
function DeskMessage:registerUser(player_data_list)
	-- print("______________registerUser________", player_data_list)
    local result = {} --返回结果
    local failed_list = {} --进桌失败列表
    local success_list = {} --进桌成功列表
    local is_bairen = self.room_config:isHundredGame()
    if not is_bairen then 
        --非百人游戏
        for _, player_data in pairs(player_data_list) do
            local uid = player_data.uid
            --创建玩家信息加到玩家管理中
            local ok = self.user_manager:addUserInfo(uid)
            if ok then 
            	table_insert(success_list,uid)
            else
            	table_insert(failed_list, player_data)
            end
        end
    else
        --百人游戏
        for _, player_data in pairs(player_data_list) do
            local uid = player_data.uid        
            table_insert(success_list,uid)
        end
    end

    result.failed_list = failed_list
    result.success_list = success_list
    local table_stats = {
	    table_type = self.table_type,
	    left_num = self.user_manager:getLeftUserNum(),
	    table_gid = self.table_gid,
	    game_type = self.game_type,
	    table_pattern = self.table_pattern,    	
    }

    result.table_stats = table_stats
    return result
end


-------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------

--进入桌子
function DeskMessage:enterReq(uid, fd, msg)
    dbglog('now enter ========================',uid, fd)
    local user = self.user_manager:getUser(uid)
    if not user then
        --第一次进入
        local is_bairen = self.room_config:isHundredGame()
        if is_bairen then 
            --如果是百人游戏）创建玩家信息加到玩家管理中        
            local ok, err_code = self.user_manager:addUserInfo(uid)        
            if not ok then 
                errlog(uid,'this player had not been registerd!!!')
                return false            
            end   
            user = self.user_manager:getUser(uid) 
            user:init(msg) --初始化玩家信息
        else
            --
            errlog(uid,'非百人场没有注册玩家this player had not been registerd!!!')
            return false                        
        end 
    else
        --断线重连进来
        if self.banker_manager then 
            --正在当庄
            if self.banker_manager:isBanker(user) then 
                self.banker_manager:setUnbanker(false) --不下庄
            end            
        end
    end
    user:setFD(fd)
    user:setDisconnect(false) --断线标识    
    if user:isPlayer() then 
        --好友服 (平台概况)
        R().friendsvr(1):send('.info_mgr', 'update_info', uid, {curr_game_type=self.game_type, curr_table_type=self.table_type})
    end         
    
    --机器人不用发送消息
    if fd == -1 then
        user:setIsRobot(true)
        self.desk:sendToUser(uid, 'table.RSP_ENTER', {result = 0})
        return false
    end
    local res = nil
    --各个游戏回调一下
    if self.desk.enterReqCallback then 
        res = self.desk:enterReqCallback(uid, msg)
    end
    -- dbglog("__enterReqCallback___",  res)
    --返回消息给请求者
    self.desk:sendToUser(uid, 'table.RSP_ENTER', res)
    --跟网关agent绑定seesion监控 网关有关闭事件进行通知
    self.desk:watchSession(uid, true)
    --玩家入桌消息推送
    self.desk:ntPlayerEnter(uid)

    return true
end

-- 请求坐下
function DeskMessage:sitDownReq(uid, fd, msg)
    dbglog("============sitDownReq=============",uid, msg.seat_id, fd)
    local user = self.user_manager:getUser(uid)
    if not user or not user:isFD(fd) then --玩家不在这个桌子里
        errlog(uid,'_____调用sitDownReq，但玩家不在这个桌子里______invalid status', fd)
        return false
    end
    local seat_id = msg.seat_id --要坐的位置
    if self.room_config:isHundredGame() then --百人游戏 玩家不可以做1，4位置
        if user:isPlayer() then 
            if seat_id==1 or seat_id == 4 then                 
                self.desk:sendToUser(uid, 'table.RSP_SITDOWN',{
                    result = error_code.SEAT_SPECIAL_SIDE,            
                })      
                return true          
            end            
        end
        if self.banker_manager then 
            local banker = self.banker_manager:getBanker() 
            if banker and banker:compare(user) then 
                self.desk:sendToUser(uid, 'table.RSP_SITDOWN',{
                    result = error_code.DOING_BANKER_PLS_LEAVE_LATER,            
                })                                
                return true
            end
        end
    end    
    --要坐的位置是否在椅子数内
	local ok, res, sitdown_coin = self.user_manager:sitDown(uid, seat_id)
	if ok then 
		if self.desk.sitDownCallback then --让各个游戏做它要做的事
			self.desk:sitDownCallback()
		end
	    self.desk:sendToUser(uid, 'table.RSP_SITDOWN',{seat_id = res})		
		--通知所有玩家有人坐下		
	    local info = user:getClientInfo()
	    self.desk:sendToAllUser('table.NTF_SITDOWN',{player = info})
		return true
	else
        -- errlog(uid,'坐下错误failed to sit down', uid, fd, seat_id)
        -- 无法坐下时，seat_id是错误码
        local des = self.room_config:getErrResult(res, sitdown_coin)
        self.desk:sendToUser(uid, 'table.RSP_SITDOWN', des)
	end
	return false
end

--站起请求
function DeskMessage:standUpReq(uid, fd, msg)
    print("_____standUpReq____", uid, fd)
    local user = self.user_manager:getUserInfo(uid)
    if not user or not user:isFD(fd) then --玩家不在这个桌子里
        errlog(uid,'_____standUpReq，但玩家不在这个桌子里______invalid status',user:getFD(), fd)
        return false
    end    
    --玩家不在位置上 
    local seat_id = msg.seat_id
    -- print("_____standUpReq__2__", uid, seat_id)
    if not user:isSeated() or user:getSeatId()~=seat_id then
        self.desk:sendToUser(uid,'table.RSP_STANDUP',{
            result = error_code.CANT_STANDUP
        })
        return true
    end
    self.user_manager:standUp(uid)
    --通知所有玩家
    self.desk:sendToAllUser('table.NTF_STANDUP', {uid=uid, seat_id=seat_id})
    self.desk:sendToUser(uid, 'table.RSP_STANDUP',{seat_id=seat_id,})


    return true
end

--玩家请求离开房间
function DeskMessage:leaveReq(uid, fd)
   dbglog("______user:getBeted()___________",uid, fd)
    local user = self.user_manager:getUser(uid)
    if not user or not user:isFD(fd) then --玩家不在这个桌子里
        errlog(uid,'_____leaveReq，但玩家不在这个桌子里______invalid status',user:getFD(), fd)
        return false
    end
    if user:getBeted() == true then
        --有押注过,暂时还不能解锁
        dbglog("leaveReq____setBeted ",user:getBeted())
        dbglog(uid,'已押注过离开', user:getFD())                
        user:setDisconnect(true)
        self.desk:sendToUser(uid, 'table.RSP_LEAVE', {result=0})      
        return true
    end
    if self.banker_manager then 
        --上庄列表处理
        local banker = self.banker_manager:getBanker()
        if self.banker_manager:isInBankerList(uid) then                            
            user:setDisconnect(true)
            self.desk:sendToUser(uid, 'table.RSP_LEAVE', {result=0})     
            --清掉上庄列表中离线的排名
            self.banker_manager:clearBankerList()
            dbglog(uid,'在上庄或上庄列表中离开',user:getName(), user:getFD(), user:getDisconnect())              
        end
        --正在当庄
        if banker:compare(user) then        
            self.desk:sendToUser(uid, 'table.RSP_LEAVE', {result=error_code.DOING_BANKER_PLS_LEAVE_LATER})      
            return true
        end
    end

    local seat_id = user:getSeatId()
    local res = nil
    if self.desk.leaveReqCallback then 
        res = self.desk:leaveReqCallback(uid) --让各个游戏做它要做的事
    end    
    if not res then 
        self.desk:sendToUser(uid, 'table.RSP_LEAVE', {result=error_code.HAD_BET_CANT_LEAVE_ERROR})
        return false
    end

    --通知玩家有人离开
    if user:isSeated() then 
        self.desk:sendToAllUser('table.NTF_LEAVE',{uid = uid, reason=1, room_name = self.room_config:getRoomName()})
    end
    self.desk:sendToUser(uid, 'table.RSP_LEAVE', {result=0})
    self.desk:watchSession(uid, false) --跟网关agent绑定seesion监控
    self.desk:sendUserLeave(uid)
    self.user_manager:removeUser(uid) 
    
    --通知分配服务器更新桌子状态
    self.desk:ntMatchsvrTableStats()
    
    if res and user:isPlayer() then
        R().hallsvr({key=uid}):send('.msg_handler', 'check_lucky_tigger',uid, self.game_type, user:getInitCoins())
        R().friendsvr(1):send('.info_mgr','update_info',uid,{curr_game_type=0})        
    end
    dbglog("____1111111111111111111111111_玩家_离开桌子___",uid)
    return true
end

--请求玩家列表
function DeskMessage:playerListReq(uid, fd, msg)
    dbglog("common playerListReq")
    local user = self.user_manager:getUserInfo(uid)
    if not user or not user:isFD(fd) then --玩家不在这个桌子里
        errlog(uid,'_____playerListReq，但玩家不在这个桌子里______invalid status',user:getFD(), fd)
        return false
    end
    local rsp_player_info_list = {}
    local user_list = self.user_manager:getUserList()
    local rich_user = self.user_manager:getSeatUser(1)
    local luck_user = self.user_manager:getSeatUser(4) 
    local luck_uid = nil
    local rich_uid = nil
    if rich_user then 
        rich_uid = rich_user:getUid()
    end
    if luck_user then 
        luck_uid = luck_user:getUid()
    end

    for uid, user in pairs(user_list) do
        if uid ~= luck_uid and uid ~= rich_uid then
            local rsp_player_info = user:getClientInfo()
            table_insert(rsp_player_info_list, rsp_player_info)
        end
    end 
    --下注金额→获胜局数→玩牌局数
    table_sort(rsp_player_info_list,function(a,b)
        if a.total_bet ~= b.total_bet then
            return a.total_bet > b.total_bet
        elseif a.win_count ~= b.win_count then
            return a.win_count > b.win_count
        else
            return a.play_count < b.play_count
        end
    end)

    --运气王置顶显示
    if luck_user then
        table_insert(rsp_player_info_list, 1, luck_user:getClientInfo())
    end
    if rich_user then
        if #rsp_player_info_list == 0 then
            table_insert(rsp_player_info_list, rich_user:getClientInfo())
        else
            table_insert(rsp_player_info_list, 2, rich_user:getClientInfo())
        end
    end

    self.desk:sendToUser(uid,'table.RSP_PLAYER_LIST',{
        players = rsp_player_info_list
    })

    return true
end


--请求聊天
function DeskMessage:chatReq(uid, fd, msg)
    local user = self.user_manager:getUserInfo(uid)
    if not user or not user:isFD(fd) then --玩家不在这个桌子里
        errlog(uid,'_____chatReq，但玩家不在这个桌子里______invalid status',user:getFD(), fd)
        return false
    end
    local curr_time = os.time() 
    --发送时间间隔
    if curr_time - user:getLoasChatTime() < 1 then
        self.desk:sendToUser(uid, 'table.RSP_CHAT',
            {result = error_code.REQ_CHAT_TOO_FAST})
        return false
    end
    user:setLoasChatTime(curr_time)
    --返回结果
    self.desk:sendToUser(uid, 'table.RSP_CHAT', {result = error_code.RSP_SUCESS})
    --推送给所有人
    local ntf = {
        uid = uid,
        type = msg.type,
        picture_id = msg.picture_id,
        content = msg.content,
        voice = msg.voice,
        content_id = msg.content_id,
        name = user:getName(),
        icon = user:getIcon(),
        sex = user:getSex(),
    }
    self.desk:sendToAllUser('table.NTF_CHAT',ntf)

    return true
end

--使用魔法表情
function DeskMessage:useMagicPictureReq(uid, fd, msg)    
    local dst_id = msg.dst_id
    local picture_id = msg.picture_id
    local user = self.user_manager:getUser(uid)
    if not user then
        errlog(uid,"REQ_USE_MAGIC_PICTURE failed you are not a src_player_info")
        return
    end
    if not user:isFD(fd) then
        errlog(uid,'__fd与user的不一样__invalid status', user:getFD(), fd)
        return
    end
    --没有坐下
    local dst_user = self.user_manager:getUser(dst_id)
    if not dst_user:isSeated() then
        self.desk:sendToUser(uid, 'table.RSP_USE_MAGIC_PICTURE', {result = error_code.USE_ITEM_NEED_POSTION})
        return
    end
    --要发送给的玩家
    local dst_user = self.user_manager:getUser(dst_id)
    if not dst_user then
        errlog(dst_id,"failed you are not a dst_player_info")
        self.desk:sendToUser(uid, 'table.RSP_USE_MAGIC_PICTURE',{
            result = error_code.TABLE_USE_MAGIC_PICTURE_MUST_SITDOWN
        })
        return
    end
    --没有坐下
    if not dst_user:isSeated() then
        errlog(uid,"player not have position")
        self.desk:sendToUser(uid, 'table.RSP_USE_MAGIC_PICTURE',{
            result = error_code.TABLE_USE_MAGIC_PICTURE_MUST_SITDOWN
        })
        return
    end
    -- src_player_info.coins = ret.curr
    self.desk:sendToUser(uid, 'table.RSP_USE_MAGIC_PICTURE',{})

    self.desk:sendToAllUser('table.NTF_USE_MAGIC_PICTURE',{
        src_id = uid,
        dst_id = dst_id,
        picture_id = picture_id,
    })
    -- dbglog("发送魔法表情成功___", uid, dst_id, picture_id)
    return true    
end

--换游戏
function DeskMessage:changeGameReq(uid, fd, msg)
    return true
end

--金币变化
function DeskMessage:changeCoinsReq(uid, fd, msg)
    local res = {
        result = 0,
        -- description = '',
    }
    self.desk:sendToAllUser('table.RSP_COINS_CHANGE',res)
    return true
end


--大厅取个人信息
function DeskMessage:personalInfoReq(uid, fd, msg)
    local user = self.user_manager:getUser(uid)
    if not user then 
        return true
    end
    if not next(msg) or not msg.uid then 
        return true
    end
    dbglog("_____personalInfoReq__msg___", msg)
    local dest_uid = msg.uid
    if dest_uid then 
        if dest_uid == 1 and self.banker_manager then 
            --取系统庄信息
            user = self.banker_manager:getBanker()
        else
            user = self.user_manager:getUser(dest_uid)
        end
    end
    if not user then 
        errlog("________error_code.INPUT_ERROR________", uid, dest_uid)
        local res = {result=error_code.INPUT_ERROR, description="玩家已离开桌子"}
        self.desk:sendToUser(uid, 'table.RSP_PERSONAL_INFO', res)
        return 
    end    
    
    local personal_info = {
        uid = dest_uid,
        name = user:getName(),
        sex = user:getSex(),
        icon = user:getIcon(),
        coins = user:getCoins(),        
        vip_level = user:getVipLevel(),
        icon_border = user:getIconBorder(),        
        is_self = false,
    }
    local ret = {}
    ret.personal_info = personal_info
    ret.is_set_down = user:isSeated()
    self.desk:sendToUser(uid, 'table.RSP_PERSONAL_INFO', ret)
    return true
end


--开财神
function DeskMessage:openCaiShenReq(uid, fd, msg)
    local user = self.user_manager:getUser(uid)
    if not user then 
        return 
    end
    local coins = msg.coins
    if self.desk:getCaiShenCoins(uid) ~= coins then
        errlog("put coins error",uid, self.desk:getCaiShenCoins(uid), coins)
        self.desk:sendToUser(uid, 'table.RSP_OPEN_CAISHEN',{result = error_code.INPUT_ERROR})
        return 
    end
    if user:getCoins() < coins then
        errlog("you have not enough coins",uid)
        self.desk:sendToUser(uid, 'table.RSP_OPEN_CAISHEN',{result = error_code.NOT_ENOUGH_COINS})
        return false
    end    
    self.desk:payCaiShenCoins(uid, coins) 
    return true
end

--请求上庄列表
function DeskMessage:bankerListReq(uid, fd, msg)
    local user = self.user_manager:getUser(uid)
    if not user then 
        return true
    end
    local banker_list = self.banker_manager:getBankerList()
    local banker = self.banker_manager:getBanker()
    local banker_info = {}
    if not banker then 
        return
    end
    local info = banker:getClientInfo()
    if banker:getUid() == 1 then --系统庄
        info.coins = 0    
    end      
    table_insert(banker_info, info)
    for i=1, #banker_list do 
        local user = self.user_manager:getUser(banker_list[i])
        local info = user:getClientInfo()      
        table_insert(banker_info, info)
    end

    self.desk:sendToUser(uid, "table.RSP_BANKER_LIST", { 
        result = error_code.RSP_SUCESS,
        banker_info = banker_info
     })

    return true
end

--请求上庄
function DeskMessage:doBankerReq(uid, fd, msg)
    local user = self.user_manager:getUser(uid)
    if not user then 
        return 
    end
    --正在庄上
    if self.banker_manager:isBanker(user) then   
        errlog(string_format("uid(%d) DOING_BANKER_ERROR", uid))     
        self.desk:sendToUser(uid, "table.RSP_DO_BANKER", { result = error_code.DOING_BANKER_ERROR })
        return true
    end
    --已在上庄列表
    if self.banker_manager:isInBankerList(uid) then   
        errlog(string_format("uid(%d) HAD_APPLY_BANKER_ERROR", uid))     
        self.desk:sendToUser(uid, "table.RSP_DO_BANKER", { result = error_code.HAD_APPLY_BANKER_ERROR })
        return true
    end
    --身上金币是否满足上庄条件
    local room_data = self.room_config:getRoomData()
    local min_banker_coins = room_data.min_banker_coins
    if min_banker_coins > user:getCoins() then
        errlog(string_format("uid(%d) NO_MONEY_FOR_DO_BANKER_ERROR", uid))     
        self.desk:sendToUser(uid, "table.RSP_DO_BANKER", { result = error_code.NO_MONEY_FOR_DO_BANKER_ERROR })
        return true
    end
    -- --如果玩家上庄，随机上庄列表中的一个机器人50%概率下庄
    -- if user:isPlayer() then

    -- end    
    self.banker_manager:addBanker(uid)
    self.desk:sendToUser(uid, "table.RSP_DO_BANKER", { result = error_code.RSP_SUCESS })

    if user:isPlayer() then 
        self:bankerListReq(uid, fd)
    end

    return true
end

--请求下庄
function DeskMessage:undoBankerReq(uid, fd, msg)
    local user = self.user_manager:getUser(uid)
    if not user then 
        return 
    end
    --是否正在当庄，如果是先放到下庄列表
    if self.banker_manager:isBanker(user) then 
        self.banker_manager:setUnbanker(true) 
        self.desk:sendToUser(uid, "table.RSP_UNDO_BANKER", { result = error_code.ACCEPT_UNDO_BANKER_EFFECTIVE_LATER })
        return true        
    end
    --从上庄列表移除
    self.banker_manager:removeBanker(uid)
    self.desk:sendToUser(uid, "table.RSP_UNDO_BANKER", { result = error_code.RSP_SUCESS  })
    --推送列表
    if user:isPlayer() then 
        self:bankerListReq(uid, fd)
    end
    return true
end


--配牌请求
function DeskMessage:configCardsReq(uid, fd, msg)    
    if not server_config.debug then 
        errlog("______配置牌无效__server_config.debug=false__")
        self.desk:sendToUser(uid, "table.RSP_CONFIG_CARDS", { result = 1, description="配牌功能未开启" })
        return 
    end   
    errlog("______config_cards__",  msg)
    local user = self.user_manager:getUser(uid)
    if not user then 
        return 
    end    
    if self.rule and self.rule.configCards then 
        self.rule:configCards(uid, msg)
    end
    self.desk:sendToUser(uid, "table.RSP_CONFIG_CARDS", { result = 0 })
end

--在线玩家概况数据
function DeskMessage:getSummaryOnline()

    if self.desk.getSummaryOnline then --不是百人场的要特殊处理
        return self.desk:getSummaryOnline() 
    end

    local user_list = self.user_manager:getUserList()
    local res = {}
    for k, user in pairs(user_list) do 
        if user:isPlayer() then 
            table_insert(res, {
                uid = user:getUid(), 
                all_bet = user:getTotalBet(), --投注
                all_win_lose =user:getWinLose(), --输赢
                all_free = user:getFree(), --台费
                all_produce =  user:getProduce(), --产出 (百人场 = 输赢+台费)
            })
        end
    end
    return res
end




return DeskMessage